//
//  DeviceInfoVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/21.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class DeviceInfoVC: RootVC {
    
    private var datas : [(String,String?)] = [("Local Number",""),
                                              ("IMEI",""),
                                              ("IMSI",""),
                                              ("SSID",""),
                                              ("Maximum Number of Clients",""),
                                              ("Wi-Fi Coverage",""),
                                              ("Local Network",""),
                                              ("IP Address",""),
                                              ("MAC Address",""),
                                              ("WAN IP Address",""),
                                              ("Software Version",""),
                                              ("Internal Version",""),
                                              ("Hardware Version","")]
    
    private var model : DeviceInfoVCInfo? {
        didSet {
            datas[0].1 = isEmpty(string:model?.msisdn) ? "----" : model!.msisdn!
            datas[1].1 = model?.imei
            datas[2].1 = model?.simImsi
            datas[3].1 = model?.sSID1
            datas[4].1 = model?.mAXAccessNum
            var wifiC = ""
            if model?.wifiCoverage != nil {
                if model!.wifiCoverage!.caseInsensitiveCompare("short_mode") == .orderedSame {
                    wifiC = "Short Range - Power Conservation"
                }else if model!.wifiCoverage!.caseInsensitiveCompare("medium_mode") == .orderedSame {
                    wifiC = "Medium Range"
                }else if model!.wifiCoverage!.caseInsensitiveCompare("long_mode") == .orderedSame {
                    wifiC = "Long Range"
                }
            }
            datas[5].1 = wifiC
            datas[6].1 = model?.localDomain
            datas[7].1 = model?.lanIpaddr
            datas[8].1 = model?.macAddress
            datas[9].1 = model?.wanIpaddr
            datas[10].1 = model?.webVersion
            datas[11].1 = model?.waInnerVersion
            datas[12].1 = model?.hardwareVersion
        }
    }
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = whiteColor
        tab.rowHeight = 50
        tab.register(DeviceInfoListCell.self, forCellReuseIdentifier: "cell")
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        return tab
    }()
    
    override func configView() {
        configNaviView(title: "Device Information")
        view.addSubview(tableView)
        net_getDeviceInfoList {[weak self] (model) in
            self?.model = model
            self?.tableView.reloadData()
        }
    }
}

extension DeviceInfoVC : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DeviceInfoListCell
        cell.refreshData(name: datas[indexPath.row].0,info:datas[indexPath.row].1)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}

fileprivate class DeviceInfoListCell : UITableViewCell {
    
    private lazy var leftLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textAlignment = .right
        label.backgroundColor = whiteColor
        label.textColor = blackColor
        return label
    }()
    
    private lazy var rightLabel: UILabel = {
        let label = UILabel()
        label.font = leftLabel.font
        label.textAlignment = .right
        label.backgroundColor = whiteColor
        label.textColor = textGray
        return label
    }()
    
    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = textGray
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configView() {
        contentView.backgroundColor = whiteColor
        selectionStyle = .none
        contentView.addSubview(leftLabel)
        contentView.addSubview(rightLabel)
        contentView.addSubview(line)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let unitWidth = (width - 8 * 3) / 5
        leftLabel.frame = CGRect(x: 8, y: 0, width: CGFloat(Int(unitWidth * 2)), height: height)
        rightLabel.frame = CGRect(x: leftLabel.right + 8, y: 0, width: CGFloat(Int(unitWidth * 3)), height: height)
        line.frame = CGRect(x: 8, y: height - 0.5, width: width - 16, height: 0.5)
    }
    
    func refreshData(name:String, info:String?) {
        leftLabel.text = name
        rightLabel.text = info
    }
}
